/*
GNU Lesser General Public License

JToggleButtonNoFocus
Copyright (C) 2000 Howard Kistler

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package com.hexidec.ekit.component;

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ButtonModel;
import javax.swing.DefaultButtonModel;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JToggleButton;
import javax.swing.border.BevelBorder;

/** Class for providing a JToggleButton that does not obtain focus
  */
public class JToggleButtonNoFocus extends JLabel
{
        Action action;
        String actionCommand;
        ButtonModel model;
        List<ActionListener> actionListener = new ArrayList<ActionListener>();
        boolean selected;


	public JToggleButtonNoFocus()                       { super();           init(); this.setRequestFocusEnabled(false); }
	public JToggleButtonNoFocus(Action a)               { action = a;          init(); this.setRequestFocusEnabled(false); }
	public JToggleButtonNoFocus(Icon icon)              { super(icon);       init(); this.setRequestFocusEnabled(false); }
	public JToggleButtonNoFocus(String text)            { super(text);       init(); this.setRequestFocusEnabled(false); }
	//public JToggleButtonNoFocus(String text, Icon icon) { super(text, icon); init(); this.setRequestFocusEnabled(false); }

       private void init(){
            this.setRequestFocusEnabled(false);
            this.setOpaque(false);
            this.setBorder(BorderFactory.createEmptyBorder());
            this.setBackground(null);
            this.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                setSelected(!isSelected());
                fireActionListener();
            }
            public void mousePressed(MouseEvent e) {}
            public void mouseReleased(MouseEvent e) {}
            public void mouseEntered(MouseEvent e) {}
            public void mouseExited(MouseEvent e) { }
        });

            this.model = new DefaultButtonModel();
            this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        }

	public boolean isFocusable()
	{
		return false;
	}

        public void setActionCommand(String cmd){
            actionCommand =cmd;
        }

        public void addActionListener(ActionListener l){
            this.actionListener.add(l);
        }

        private void fireActionListener(){
            ActionEvent e = new ActionEvent(this, 1, actionCommand);
            for(ActionListener l: this.actionListener){
                l.actionPerformed(e);
            }
            if(action!=null){
                action.actionPerformed(e);
            }
        }

        public ButtonModel getModel(){
            return model;
        }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        if(selected){
            this.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
        }else{
            this.setBorder(BorderFactory.createEmptyBorder());
        }
        this.selected = selected;
    }



}
